/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.DataBaseConnector;
import de.fuberlin.kosmik.gui.view.MapFrame;

/**
 * This activity shows a saved Track in a MapFrame.
 * <p>
 * The id of the track has to be sent in the intent with the key TRACK_ID.
 */
public class ShowSavedTrackActivity extends TrackActivity implements OnClickListener {

	/**
	 * Key of the track id in the bundle.
	 */
	public static final String TRACK_ID = "track_id";

	private long trackId;
	private String trackName;
	private ImageButton btnShowList;
	private MapFrame mapFrame;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_old_track);
		trackId = getIntent().getLongExtra(TRACK_ID, -1);
	}

	@Override
	protected void onStart() {
		super.onStart();
		DataBaseConnector db = getDataBase();
		trackName = db.getNameFromTrack(trackId);
		findViewsAndSetListener();
		mapFrame.setTrackId(trackId);
	}

	@Override
	protected void onStop() {
		super.onStop();
		invalidateViewsAndListener();
	}

	private void findViewsAndSetListener() {
		btnShowList = (ImageButton) findViewById(R.id.action_bar_show_list_button);
		btnShowList.setOnClickListener(this);
		mapFrame = (MapFrame) findViewById(R.id.map_frame);
		TextView title = (TextView) findViewById(R.id.action_bar_title);
		title.setText(trackName);
	}

	private void invalidateViewsAndListener() {
		btnShowList = null;
		List<?> overlays = mapFrame.getOverlays();
		if (overlays != null)
			overlays.clear();
		mapFrame = null;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == btnShowList.getId()) {
			Intent intent = new Intent(this, TrackInfoActivity.class);
			intent.putExtra(TrackInfoActivity.TRACK_ID, trackId);
			intent.putExtra(TrackInfoActivity.IS_SAVED_TRACK, true);
			startActivity(intent);
		}

	}

}
